package com.swhy

import com.utils._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}
import org.codehaus.jackson.map.ObjectMapper

import java.net.URI
import java.util.UUID


object ChgeProdSaleRptTtlTtz {
  def main(args: Array[String]): Unit = {
    if (args.length != 3) {
      println(
        """
          |com.swhy.CompanyInfo
          |params:
          | InputDate    刷数日期
          | OutputPath   输出路径
          | SplitNum     文件生成数量
        """.stripMargin)
      sys.exit()
    }
    val conf = new Configuration()
    val Array(inputDt, outputPath, splitNum) = args
    val sparkConf = new SparkConf().setAppName(this.getClass.getName)
    val sc = new SparkContext(sparkConf)
    val hiveContext = new HiveContext(sc)
    //改下方的sql为需要装换成jason的sql
    val allcompany =
      s"""
     select
      distinct
      '970191/970187' as prod_code,
      '天天增/天天利' as prod_name,
      a.cust_no,
      a.cust_name,
      a.capi_acc,
      nvl(a.mgr_serv,'') as mgr_cd,
      nvl(a.mgr_dev,'') as mgr_dev,
      nvl(a.mgr_sign,'') as mgr_sign,
      nvl(a.brch_org_level_cd_serv,'')  as brch_org_level_cd,
      nvl(a.brch_org_level_cd_dev,'')  as brch_org_level_cd_dev,
      nvl(a.brch_org_level_cd_sign,'')  as brch_org_level_cd_sign,
      nvl(a.brch_org_cd_serv,'')  as brch_org_cd,
      nvl(a.brch_org_cd_dev,'')  as brch_org_cd_dev,
      nvl(a.brch_org_cd_sign,'')  as brch_org_cd_sign,
      if(c.asset_acct is null,'0','1') as  prod4_is_buy,
      a.latest_risk_grad,
      a.cust_grad,
      a.cust_type,
      nvl(round(a.total_asset),0) as total_asset ,--总资产
      if(b.asset_acct is null,'0','1') as prod4_bjz_flag,--  保证金产品
      round(nvl(f.xjlzc,0))  as  prod4_cap_bal,--现金类基金资产
      nvl(f.xjlzc_mx,'[]')  as  prod4_cap_bal_detail,--现金类资产
      round(nvl(g.hb_asset,0)) as prod4_hb_asset
      from  app_data.app_dao_acct_info_d_q  a
      left join APP_DATA.TEMP_ASSET_CAP_RIGHTS_DAOP b
      on a.capi_acc=b.asset_acct
      left join app_data.app_daop_recmd_aft_purc_z  c
      on a.capi_acc =c.asset_acct
      and c.prod_id='天天利/天天增'
      left join app_data.xjlzc_daop_temp  f
      on a.capi_acc=f.asset_acct
      left join (select e.asset_acct
                    ,sum(case when e.prod_type_cd = 'F23' then Secu_Mrkt_Val_Rmb else 0 end ) as hb_asset --货币类资产
                    ,case when sum(case when e.prod_type_cd = 'F23' then Secu_Mrkt_Val_Rmb else 0 end ) > 0 then '1' else '0' end  as pos_hb_flag
                    ,sum(case when e2.prod_name  like '%短债%' then Secu_Mrkt_Val_Rmb else 0 end ) as dz_asset --短债类资产
                    ,case when sum(case when e2.prod_name  like '%短债%' then Secu_Mrkt_Val_Rmb else 0 end )>0 then '1' else '0' end as pos_dz_flag
                from sum_data.sum_t98_asset_pos_acct_z e
                left join pdm_data.pdm_t02_prod_q e2
                  on e.prod_id = e2.prod_id
                 and e2.biz_date = ${inputDt}
               where e.biz_date = ${inputDt}
                 and ( e2.prod_type_cd = 'F23' or e2.prod_name like '%短债%')
               group by e.asset_acct)  g
       on a.capi_acc=g.asset_acct
      where  a.biz_date=${inputDt}
      """.stripMargin
    print(allcompany)
    val frame  = hiveContext.sql(allcompany)
    val result = frame.rdd.map(x => {
      var cominfo = new StringBuilder()

      val prod_code = x.getAs[String]("prod_code")
      val prod_name = x.getAs[String]("prod_name")
      val cpai_acc = x.getAs[String]("capi_acc")
      val cust_no = x.getAs[String]("cust_no")
      val cust_name = x.getAs[String]("cust_name")
      val latest_risk_grad = x.getAs[String]("latest_risk_grad")
      val cust_grad = x.getAs[String]("cust_grad")
      val cust_type = x.getAs[String]("cust_type")
      val mgr_cd = x.getAs[String]("mgr_cd")
      val mgr_dev = x.getAs[String]("mgr_dev")
      val mgr_sign = x.getAs[String]("mgr_sign")
      val brch_org_cd = x.getAs[String]("brch_org_cd")
      val brch_org_cd_dev = x.getAs[String]("brch_org_cd_dev")
      val brch_org_cd_sign = x.getAs[String]("brch_org_cd_sign")
      val brch_org_level_cd = x.getAs[String]("brch_org_level_cd")
      val brch_org_level_cd_dev = x.getAs[String]("brch_org_level_cd_dev")
      val brch_org_level_cd_sign = x.getAs[String]("brch_org_level_cd_sign")
      val total_asset =scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("total_asset"))
      val total_asset1=total_asset.bigDecimal
      val prod4_bjz_flag = x.getAs[String]("prod4_bjz_flag")
      val prod4_is_buy =x.getAs[String]("prod4_is_buy")
      val prod4_cap_bal =scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("prod4_cap_bal"))
      val prod4_cap_bal1=prod4_cap_bal.bigDecimal
      val prod4_cap_bal_detail = x.getAs[String]("prod4_cap_bal_detail")
      val prod4_hb_asset =scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("prod4_hb_asset"))
      val prod4_hb_asset1=prod4_hb_asset.bigDecimal
      val _id = new ID()
      val index = new Index()
      val companyInfo = new ChgeProdSaleRptTtlTtz()

      _id.set_id(UUID.randomUUID().toString)
      index.setIndex(_id)
      companyInfo.setProdCode(prod_code)
      companyInfo.setProdName(prod_name)
      companyInfo.setCapiAcc(cpai_acc)
      companyInfo.setCustNo(cust_no)
      companyInfo.setCustName(cust_name)
      companyInfo.setMgrCd(mgr_cd)
      companyInfo.setBrchOrgCd(brch_org_cd)
      companyInfo.setBrchOrgLevelCd(brch_org_level_cd)
      companyInfo.setTotalAsset(total_asset1)
      companyInfo.setMgrSign(mgr_sign)
      companyInfo.setProd4CapBalDetail(prod4_cap_bal_detail)
      companyInfo.setMgrDev(mgr_dev)
      companyInfo.setBrchOrgCdDev(brch_org_cd_dev)
      companyInfo.setBrchOrgCdSign(brch_org_cd_sign)
      companyInfo.setBrchOrgLevelCdDev(brch_org_level_cd_dev)
      companyInfo.setBrchOrgLevelCdSign(brch_org_level_cd_sign)
      companyInfo.setProd4IsBuy(prod4_is_buy)
      companyInfo.setLatestRiskGrad(latest_risk_grad)
      companyInfo.setCustGrad(cust_grad)
      companyInfo.setCustType(cust_type)
      companyInfo.setMgrDev(mgr_dev)
      companyInfo.setMgrSign(mgr_sign)
      companyInfo.setProd4BjzFlag(prod4_bjz_flag)
      companyInfo.setProd4HbAsset(prod4_hb_asset1)
      companyInfo.setProd4CapBal(prod4_cap_bal1)
      val mapper = new ObjectMapper()

      val indexJson = mapper.writeValueAsString(index) //企业自增编码json串
      val companyInfoJson = mapper.writeValueAsString(companyInfo) //企业信息串

      cominfo = cominfo.append(indexJson + "\n" + companyInfoJson) //json结果串
      cominfo.toString()
    })
    val path = new Path(outputPath) //创建文本输出路径
    val fs = FileSystem.get(URI.create(outputPath), conf)
    if (fs.exists(path)) {
      fs.delete(path, true)
    }
    result.coalesce(splitNum.toInt, true).saveAsTextFile(outputPath)
    sc.stop()
  }
}
